package com.example.sample.Infrastructure.Thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLock implements Runnable {

    private  int num = 10;
    private boolean isSuccessful;

    @Override
    public void run() {

        while (num > 0) {
            Buy();
        }
    }
    private static Lock lock =new ReentrantLock();
    private void Buy() {

        lock.lock();

        if (num <= 0) {
            isSuccessful = false;
            return;
        }

        System.out.println(Thread.currentThread().getName() + ",数量" + num--);
        lock.unlock();
    }
}
